import { createRouter, createWebHistory,createWebHashHistory, RouteRecordRaw } from 'vue-router'
import { useAuthStore } from '../store/authStore'
import KeepAlive from '../pages/demo/KeepAlive.vue';
const modules = import.meta.glob("../pages/**/**.vue");
import NProgress from 'nprogress'

import Home from '../pages/home/Home.vue';

const whiteUrl=['/hook','/bmap','/amap','/vmodel','/directive','/login','/wechatlogin','/week','/fileAdd','/map','/slot','/table','/editor','/provide','/tree','/pdf','/down','/keepalive'];
const keepAlivePages=["company/list"];
const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'Home',
    component: () => import('../pages/home/Home.vue'),
    children: [
      {
        path: '/index',
        name: 'Index',
        meta: {
          title:"首页"
        },
        component: () => import('../pages/home/Index.vue')
      },
      {
        path: '/alvie',
        name: 'Alvie',
        meta: { keepAlive: true },
        component: () => import('../pages/home/Alvie.vue')
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    meta: {
      keepAlive: false,title:"登录"
    },
    component: () => import('../pages/login/Login.vue')
  },
  {
    path: '/wechatlogin',
    name: 'wechatlogin',
    meta: {
      keepAlive: false
    },
    component: () => import('../pages/login/WechatLogin.vue')
  },
  
  {
    path: '/week',
    name: 'Week',
    meta: {
      keepAlive: false
    },
    component: () => import('../pages/Week.vue')
  },
  {
    path: '/fileAdd',
    name: 'FileAdd',
    meta: {
      keepAlive: false
    },
    component: () => import('../pages/demo/FileAdd.vue')
  },
  // {
  //   path: '/map',
  //   name: 'Map',
  //   meta: {
  //     keepAlive: false
  //   },
  //   component: () => import('../pages/demo/Map.vue')
  // },
  {
    path: '/slot',
    name: 'Slot',
    meta: {
      keepAlive: false
    },
    component: () => import('../pages/demo/Slot.vue')
  },
  {
    path: '/table',
    name: 'Table',
    meta: {
      keepAlive: false
    },
    component: () => import('../pages/demo/Table.vue')
  },
  {
    path: '/editor',
    name: 'editor',
    meta: {
      keepAlive: false
    },
    component: () => import('../pages/demo/Editor.vue')
  },
  {
    path: '/provide',
    name: 'provide',
    meta: {
      keepAlive: false
    },
    component: () => import('../pages/demo/Provide.vue')
  },
  {
    path: '/tree',
    name: 'tree',
    meta: {
      keepAlive: false
    },
    component: () => import('../pages/demo/Tree.vue')
  },
  {
    path: '/amap',
    name: 'amap',
    meta: {
      keepAlive: false
    },
    component: () => import('../pages/demo/Amap.vue')
  },
  {
    path: '/bmap',
    name: 'bmap',
    meta: {
      keepAlive: false
    },
    component: () => import('../pages/demo/Bmap.vue')
  },
  {
    path: '/hook',
    name: 'hook',
    meta: {
      keepAlive: false
    },
    component: () => import('../pages/demo/Hook.vue')
  },
  // {
  //   path: '/pdf',
  //   name: 'pdf',
  //   meta: {
  //     keepAlive: false
  //   },
  //   component: () => import('../pages/demo/Pdf.vue')
  // },
  {
    path: '/down',
    name: 'down',
    meta: {
      keepAlive: false
    },
    component: () => import('../pages/demo/Down.vue')
  },
  {
    path: '/keepAlive',
    name: 'KeepAlive',
    meta: {
      keepAlive: false
    },
    component: () => import('../pages/demo/KeepAlive.vue')
  },
  {
    path: '/vmodel',
    name: 'vmodel',
    meta: {
      keepAlive: false
    },
    component: () => import('../pages/demo/VModel.vue')
  },
  {
    path: '/directive',
    name: 'directive',
    beforeEnter(to, from, next) {
      console.log('独享守卫', to, from)
      next()
    },
    meta: {
      keepAlive: false
    },
    component: () => import('../pages/demo/Directive.vue')
  }
  

  
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

//路由守卫  全局守卫（前置守卫beforeEach、后置守卫）  路由独享守卫  路由组件内守卫

let isAddRouter=false

router.afterEach((to,from)=>{

  console.log("路由后置守卫",to);
  
 
  // console.log(from);

  document.title=to?.meta?.title as string || "乐康养老";
  NProgress.done();
 
})


//路由全局 前置守卫  防止浏览器刷新
router.beforeEach(async (to, from) => {

  NProgress.start();

  console.log("路由前置守卫");
  //to 目标页面
  //from 从哪个页面过来
  //next  放行   return 

  //如果token 为空  跳转到登录页面
  const authStore = useAuthStore();
  
  if (whiteUrl.includes(to.path)) {
    return true
  }
  if (!authStore.token) {
    return '/login'
  }

  // let len = router.getRoutes().length;
  // if (len > 2) return true;

  if(isAddRouter) return true;


  isAddRouter=true;

  const res = await authStore.getMenu();

  res.forEach(item => {

    item.children.forEach(child => {

      if (child.pathName) {

        let component = modules[`../pages/${item.url}/${child.pathName}.vue`];

        console.log(`${child.name}../pages/${item.url}/${child.pathName}.vue`, `${item.url}/${child.url}`);

        let path:string=`${item.url}/${child.url}`;

        router.addRoute("Home", {
          path,
          name: child.url + child.id,
          meta:{
            keepAlive:keepAlivePages.includes(path),title:child.name
          },
          component: component
        })


      }



    })


  })



 

  return (to.fullPath)


   


  //实现动态 路由

  // router.addRoute()




})



export default router